package xyz.hubery.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.hubery.domain.CashRecharge;
import xyz.hubery.domain.CashWithdrawAuditRecord;
import xyz.hubery.domain.CashWithdrawals;
import xyz.hubery.domain.CoinWithdraw;
import xyz.hubery.model.R;
import xyz.hubery.model.SellGCNParam;
import xyz.hubery.service.CashWithdrawalsService;

@RestController
@RequestMapping("/cashWithdrawals")
@Api(tags = "GCN提现")
public class CashWithdrawalController {

    @Autowired
    private CashWithdrawalsService cashWithdrawalService;

    @GetMapping("/records")
    @ApiOperation("获取提现记录")
    public R<Page<CashWithdrawals>> findPage(Page<CashWithdrawals> page,
                                             Long userId,
                                             String userName,
                                             String mobile,
                                             Byte status,
                                             Integer numMin,
                                             Integer numMax,
                                             String startTime,
                                             String endTime
                                             ) {
        Page<CashWithdrawals> pages = cashWithdrawalService.findPage(page, userId, userName, mobile, status, numMin, numMax, startTime, endTime);
        return R.ok(pages);
    }


    @GetMapping("/user/records")
    @ApiOperation("分页查询用户的提现记录")
    public R<Page<CashWithdrawals>> findCashWithdrawalsPage(Page<CashWithdrawals> page, Byte status) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<CashWithdrawals> cashWithdrawalsPage = cashWithdrawalService.findCashWithdrawalsPage(page,userId,status);
        return R.ok(cashWithdrawalsPage);
    }

    @PostMapping("/sell")
    @ApiOperation("GCN提现")
    public R<Boolean> sell(@RequestBody @Validated SellGCNParam param) {
        Long userId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean b = cashWithdrawalService.sell(userId, param);
        return b?R.ok():R.fail("提现失败");
    }


    @PostMapping("/updateWithdrawalsStatus")
    @ApiOperation(value = "现金提现审核")
    public R updateWithdrawalsStatus(@RequestBody @Validated CashWithdrawAuditRecord cashWithdrawAuditRecord) {
        Long adminId = Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        boolean b = cashWithdrawalService.updateStatus(adminId, cashWithdrawAuditRecord);
        return b ? R.ok() : R.fail("审核失败");
    }



}
